<?php
/**
 * This file houses the code which bootstraps the application.  It creates some initial path and URL related constants and includes the needed files to prepare the application.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package	Framework
 * @subpackage Library
 */

// turn on output buffering
ob_start();

// make sure a timezone is set
if (function_exists('date_default_timezone_set')) {
	date_default_timezone_set('America/Chicago');
}

// Turn off xdebug if running; we'll display our own error messages, thanks!
if (function_exists('xdebug_disable')) {
	xdebug_disable();
}

if (!defined('IS_TEST')) {
	/** 
	 * If set to true, all database connections will use the test database.
	 */
	define('IS_TEST', false);
}

if (!defined('DISABLE_CACHE')) {
	/**
	 * If set to true, the Cache object will throw a CacheDisabledException when used.
	 */
	define('DISABLE_CACHE', true);
}

// load all the config files for the framework
$di = new DirectoryIterator(PATH_CONFIG);
foreach ($di as $configFile) {
	$filename = $configFile->getFilename();
	if (!$configFile->isDot() && substr($filename, strlen($filename) - 4, 4) == '.php' && $filename != 'Routes.php') {
		require(PATH_CONFIG . '/' . $filename);
	}
}
$di = null;
unset($di);

/**
 * Include the AutoloadException class because it is used by the autoloader.
 */
require_once($paths['framework']['exceptions'] . '/AutoloadException.php');

/**
 * Include the MemcacheException class because it is used by the Cache class.
 */
require_once($paths['framework']['exceptions'] . '/MemcacheException.php');

/**
 * Include the CacheDisabledException class because it is used by the Cache class.
 */
require_once($paths['framework']['exceptions'] . '/CacheDisabledException.php');

/**
 * Include the StringUtility class as this is pretty much used all over the app and we don't have an autoloader setup yet.
 */
require_once($paths['framework']['library'] . '/StringUtility.php');

/**
 * Include the Cache class as this is pretty much used all over the app and we don't have an autoloader setup yet.
 */
require_once($paths['framework']['library'] . '/Cache.php');

/**
 * Include the various error, exception, & autoloader class.
 */
require_once($paths['framework']['library'] . '/Handler.php');

// set error & exceptions handlers
if (IS_CLI_SCRIPT === true) {
	set_error_handler('Handler::cliErrors');
	set_exception_handler('Handler::cliExceptions');
} else {
	set_error_handler('Handler::errors');
	set_exception_handler('Handler::exceptions');
}

// set the autoloader
if (function_exists('__autoload')) {
	spl_autoload_register('__autoload');
}
spl_autoload_register('Handler::autoloading');

/**
 * Include Routes
 */
require_once(PATH_CONFIG . '/Routes.php');

// set some context values
if (!defined('FULL_URL') && IS_CLI_SCRIPT === false) {
	/**
	 * @ignore
	 */
	$accessMethod = ($_SERVER['SERVER_PORT'] == 80) ? 'http://' : 'https://';

	/**
	 * The full URL currently being accessed.
	 */
	define('FULL_URL',  $accessMethod . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);

	SiteContext::$baseUrl = filter_var($accessMethod . $_SERVER['SERVER_NAME'], FILTER_SANITIZE_URL);
}

/**
 * This allows for proper reflection of files without executing their contents.
 *
 * @param string $file
 *
 * @return void
 */
function php_check_syntax_external($file) {
	php_check_syntax($file);
}